package com.mallsystem.goodsservice.controller;

import com.github.pagehelper.PageInfo;
import com.mallsystem.common.annotation.Log;
import com.mallsystem.common.bean.Goods;
import com.mallsystem.common.bean.RespBean;
import com.mallsystem.common.bean.Score;
import com.mallsystem.common.exception.AllException;
import com.mallsystem.common.service.GoodsService;
import com.mallsystem.common.service.ScoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@CrossOrigin
@RequestMapping("/goods")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private ScoreService scoreService;

    //添加
    @PostMapping
    @Log(name = "添加商品")
    public RespBean insert(@RequestBody Goods goods) throws AllException {
        goods.setScore(5F);
        goodsService.insert(goods);
        return RespBean.ok("添加成功");
    }
    //删除
    @DeleteMapping("/{id}")
    @Log(name = "删除商品")
    public RespBean delete(@PathVariable("id") Integer id) throws AllException {
        goodsService.delete(id);
        return RespBean.ok("删除成功");
    }

    //修改
    @PutMapping
    @Log(name = "修改商品")
    public RespBean update(@RequestBody Goods goods) throws AllException {
        goodsService.update(goods);
        return RespBean.ok("修改成功");
    }
    //分页条件查询
    @GetMapping
    @Log(name = "查询商品")
    public RespBean selectByPage(Goods condition,Integer pageNum,Integer pageSize){
        pageNum = pageNum == null ? 1 : pageNum;
        pageSize = pageSize == null ? 1 : pageSize;
        PageInfo<Goods> pageInfo = goodsService.selectByCondition(condition, pageNum, pageSize);
        return RespBean.ok("查询成功",pageInfo);
    }
    @GetMapping("/search")
    @Log(name = "查询所有1")
    public RespBean selectByPage1(Goods condition,Integer pageNum,Integer pageSize){
        pageNum = pageNum == null ? 1 : pageNum;
        pageSize = pageSize == null ? 1 : pageSize;
        PageInfo<Goods> pageInfo = goodsService.selectByCondition1(condition, pageNum, pageSize);
        return RespBean.ok("查询成功",pageInfo);
    }
    //根据id查询
    @GetMapping("/{id}")
    @Log(name = "查询商品")
    public RespBean selectById(@PathVariable("id") Integer id){
        Goods goods = goodsService.selectById(id);
        return RespBean.ok("查询成功",goods);
    }
    //修改状态
    @PutMapping("/status")
    @Log(name = "修改状态")
    public RespBean updateStatus(@RequestBody Goods goods) throws AllException {
        goodsService.updateStatus(goods);
        return RespBean.ok("状态修改成功");
    }
    @PostMapping("/score")
    @Log(name = "评价商品")
    public RespBean insertScore(@RequestBody Score score) throws AllException {
        scoreService.insert(score);
        return RespBean.ok("评价成功！！");
    }
}
